package com.asiainfo.dacp.datastash.broker.schedule.buildinjob;

import com.asiainfo.dacp.core.BeanFactory;
import com.asiainfo.dacp.datastash.broker.facade.service.UnitInitService;
import com.asiainfo.dacp.datastash.broker.facade.service.UnitLoaderService;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogVo;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogger;
import com.asiainfo.dacp.datastash.broker.util.LeaderElection;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 每10分钟执行一次，
 * 初始化单元的配置
 *
 * @author MeiKefu
 * @date 2017-11-8
 */
public class StashUnitInitJob implements Job {

    private String clzName = this.getClass().getSimpleName();

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        final UnitInitService stashUnitInitService = BeanFactory.getBean(UnitInitService.class);
        //加载内存的StashUnitList
        
        /**
         * 增量更新，后续考虑增量更新机制，表扩字段
         * 
         * */
        stashUnitInitService.initUnit();
        stashUnitInitService.updateUnitBatchNo();

        //实现选主，由主来完成这个服务的调用
        /*
        try {
            new LeaderElection(new LeaderSelectorListenerAdapter(){
                public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
                    StashTraceLogger tracelog = BeanFactory.getBean(StashTraceLogger.class);
                    tracelog.debug(new StashTraceLogVo(clzName,"unitInitJob:election","选主成功"));
                    stashUnitInitService.updateUnitBatchNo();

                    UnitLoaderService unitLoaderService = BeanFactory.getBean(UnitLoaderService.class);
                    unitLoaderService.unitLoad();

                    stashUnitInitService.updateWaitQueue();
                }
            }).startup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        */
    }
}
